package com.example.vv.model.setupTree;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.example.vv.R;
import com.example.vv.adapter.BaseRecyclerViewAdapter;
import com.example.vv.adapter.RecyclerViewHolder;
import com.example.vv.fragmentWork.entity.TreeEntity;
import com.example.vv.fragmentWork.mvp.MvpFragment;

import com.example.vv.model.setupTree.base.contract.SetupTreeContract;
import com.example.vv.model.setupTree.base.presenter.SetupTreePresenter;
import com.example.vv.model.setupTreeItem.STIFragment;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;


public class SetupTreeFragment extends MvpFragment<SetupTreeContract.View, SetupTreePresenter> implements SetupTreeContract.View {

    String id;
    int index = -1;

    public SetupTreeFragment(String id) {
        this.id = id;
    }

    @BindView(R.id.setup_recycler)
    RecyclerView setupRecycler;
    @BindView(R.id.setup_fragment)
    FrameLayout setupFragment;

    @Override
    public int initLayout() {
        return R.layout.fragment_setup_tree;
    }

    @Override
    public SetupTreePresenter initPresenter() {
        return new SetupTreePresenter();
    }

    @Override
    public void initView() {

    }

    @Override
    public void lazyLoad() {
        presenter.setTreeTitle("https://www.wanandroid.com/"+id+"/json");
    }

    @Override
    public void initData() {

    }

    @Override
    public void doBusiness() {

    }

    @Override
    public View titleBar() {
        return null;
    }


    @Override
    public Context getMContext() {
        return getActivity();
    }

    @Override
    public void getTreeTitle(List<TreeEntity> treeEntityList) {

        STIFragment stiFragment = new STIFragment(treeEntityList.get(0).getName());
        FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.setup_fragment,stiFragment);
        EventBus.getDefault().postSticky(treeEntityList.get(0).getChildren());
        transaction.commit();
        BaseRecyclerViewAdapter<TreeEntity> adapter = new BaseRecyclerViewAdapter<TreeEntity>(getActivity(),treeEntityList, R.layout.item_project) {
            @Override
            protected void onBindData(RecyclerViewHolder holder, TreeEntity bean, int position) {
                TextView textView = (TextView) holder.getView(R.id.tv_class);
                textView.setText(bean.getName());
                if (index==position) {
                    textView.setBackgroundResource(R.drawable.bg_project);
                    textView.setTextColor(Color.parseColor("#ffff0505"));
                }else {
                    textView.setBackgroundResource(R.drawable.bg_project_select);
                    textView.setTextColor(Color.parseColor("#000000"));
                }
            }
        };
        adapter.setOnItemClickListener(new BaseRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView.Adapter adapter, View v, int position) {
                index = position;
                adapter.notifyDataSetChanged();
                STIFragment stiFragment = new STIFragment(treeEntityList.get(position).getName());
                FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.setup_fragment,stiFragment);
                EventBus.getDefault().postSticky(treeEntityList.get(position).getChildren());
                transaction.commit();
            }
        });
        setupRecycler.setAdapter(adapter);
    }
}